﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLDBHYT.DAO;
using QLDBHYT.VO;
using QLDBHYT.BUS;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.ReportSource;

namespace QLDBHYT
{
    public partial class frmPhieuNhap : Form
    {
        private PhieunhapBUS  phieunhapBUS;
        public frmPhieuNhap()
        {
            InitializeComponent();
        }

        private void frmPhieuNhap_Load(object sender, EventArgs e)
        {
            GetNhomPC();
            GetKy();
            phieunhapBUS = new PhieunhapBUS();
            flex.Cols["nhomthuoc"].ComboList = Properties.Settings.Default.Nhomthuoc;
           
        }
              
        private void GetNhomPC()
        {
            NhomPCBUS nhomPCBUS = new NhomPCBUS();
            cboNhomCP.ClearItems();
            cboNhomCP.DefColWidth = 300;
            cboNhomCP.AllowSort = true;
            cboNhomCP.DataSource = nhomPCBUS.GetListNhomPC();
        }
        private void GetKy()
        {
            NhomPCBUS nhomPCBUS = new NhomPCBUS();
            cboKy.ClearItems();
            cboKy.DefColWidth = 100;
            cboKy.AllowSort = true;
            cboKy.DataSource = nhomPCBUS.GetListKy();
        }

        private void btnTimkiem_Click(object sender, EventArgs e)
        {
           
            List<PhieunhapVO> lstPhieuNhap;
            if (cboKy.SelectedValue == null)
            {
                MessageBox.Show("Nhập kỳ");
                return;
            }
            if (cboNhomCP.SelectedValue == null)
            {
                MessageBox.Show("Nhập nhóm CP");
                return;
            }
            Cursor.Current = Cursors.WaitCursor;
            if ( Convert.ToInt16(cboKy.Columns["Ky"].Value)  == 1)
            {
                flex.Cols["Nhap"].AllowEditing = true; 
            }
            else
            {
                flex.Cols["Nhap"].AllowEditing = false; 
            }
            string condition = "";
            if (txtTenhieu.Text != "")
            {
                condition = condition + " and Ten_hieu like N'%" + txtTenhieu.Text + "%'";
            }
            if (txtMahieu.Text  != "")
            {
                condition = condition + " and a.ma_hieu like '%" + txtMahieu.Text + "%'";
            }
            if (Convert.ToInt16(cboKy.Columns["Ky"].Value) == 1)
            {

                lstPhieuNhap = phieunhapBUS.GetListPhieuNhap(Convert.ToInt32(cboKy.Columns[1].Value),
                    Convert.ToInt32(cboKy.Columns[2].Value), "('" + cboNhomCP.Columns[1].Value.ToString().Trim()+"')",
                    "", dtDenngay.Value.Month + "/" + dtDenngay.Value.Day + "/" + dtDenngay.Value.Year, condition);
            }
            else
            {
                lstPhieuNhap = phieunhapBUS.GetListPhieuNhap(Convert.ToInt32(cboKy.Columns[1].Value),
                    Convert.ToInt32(cboKy.Columns[2].Value), "('" + cboNhomCP.Columns[1].Value.ToString().Trim() + "')",
                    dtTungay.Value.Month + "/" + dtTungay.Value.Day + "/" + dtTungay.Value.Year ,
                    dtDenngay.Value.Month + "/" + dtDenngay.Value.Day + "/" + dtDenngay.Value.Year, condition);
            }
            flex.Rows.Count = 1;
            int i = 1;


            foreach (PhieunhapVO phieunhapVO in lstPhieuNhap)
            {


                flex.Rows.Count = flex.Rows.Count + 1;
                flex.Rows[i]["STT"] = i;
                flex.Rows[i]["mahieu"] = phieunhapVO.Mahieu;
                if (phieunhapVO.Sldauky != 0) { flex.Rows[i]["Sldauky"] = phieunhapVO.Sldauky; }
                if (phieunhapVO.Slnhap != 0)
                {
                    flex.Rows[i]["Slnhap"] = flex.Rows[i]["Slnhapcu"] = phieunhapVO.Slnhap;
                   
                }
                if (phieunhapVO.Slxuatdc != 0)
                {
                    flex.Rows[i]["Slxuatdc"] = flex.Rows[i]["Slxuatdccu"] = phieunhapVO.Slxuatdc;

                }
                if (phieunhapVO.Slxuat != 0) { flex.Rows[i]["Slxuat"] = phieunhapVO.Slxuat; }
                if (phieunhapVO.Slcuoiky != 0) { flex.Rows[i]["Slcuoiky"] = phieunhapVO.Slcuoiky; }
                flex.Rows[i]["Tenhieu"] = phieunhapVO.Tenhieu;
                flex.Rows[i]["Dvt"] = phieunhapVO.Dvt;
                flex.Rows[i]["hamluong"] = phieunhapVO.Hamluong;
                flex.Rows[i]["Nuocsx"] = phieunhapVO.Nuocsx;
                flex.Rows[i]["Bietduoc"] = phieunhapVO.Bietduoc;
                flex.Rows[i]["nhomthuoc"] = phieunhapVO.Nhomthuoc;
                flex.Rows[i]["Bietduoccu"] = phieunhapVO.Bietduoc;
                flex.Rows[i]["nhomthuoccu"] = phieunhapVO.Nhomthuoc;
                if (phieunhapVO.Gia != 0) { flex.Rows[i]["Gia"] = phieunhapVO.Gia; }
                if (phieunhapVO.Gia_bhxh != 0) { flex.Rows[i]["Giabhxh"] = phieunhapVO.Gia_bhxh; }

                if (phieunhapVO.Slcuoiky == 0)
                {
                    C1.Win.C1FlexGrid.CellStyle rs = this.flex.Styles.Add("rowcolor");
                    rs.ForeColor = Color.Blue;
                    flex.Rows[i].Style = rs;
                }
                i++;
            }
            Cursor.Current = Cursors.Default;
        }

        private void cmdThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmdGhi_Click(object sender, EventArgs e)
        {
            PhieunhapVO phieunhapVO = new PhieunhapVO();
            //int slxuat;
            Cursor.Current = Cursors.WaitCursor;
            for (int i = 1; i < flex.Rows.Count; i++)
            {
                if (Convert.ToInt32(flex.Rows[i]["Slnhap"]) != Convert.ToInt32(flex.Rows[i]["Slnhapcu"]) || Convert.ToInt32(flex.Rows[i]["slxuatdc"]) != Convert.ToInt32(flex.Rows[i]["slxuatdccu"]))
                {

                    phieunhapVO.Sttnh = cboNhomCP.Columns[1].Value.ToString();
                    phieunhapVO.Mahieu = flex.Rows[i]["Mahieu"].ToString();
                    phieunhapVO.Thang = Convert.ToInt32(cboKy.Columns[1].Value);
                    phieunhapVO.Nam = Convert.ToInt32(cboKy.Columns[2].Value);
                    phieunhapVO.Slnhap = Convert.ToInt32(flex.Rows[i]["slNhap"]);
                    phieunhapVO.Nhomthuoc = flex.Rows[i]["nhomthuoc"].ToString();
                    phieunhapVO.Ngaynhap = Convert.ToDateTime(dtNgayNhap.Value.ToShortDateString()) ;
                    phieunhapVO.Ngayxuat = Convert.ToDateTime(dtNgayNhap.Value.ToShortDateString());
                    phieunhapVO.Sl = Convert.ToDouble(flex.Rows[i]["Nhap"]);
                    phieunhapVO.Slx = Convert.ToDouble(flex.Rows[i]["xuat"]);
                    phieunhapVO.Slxuatdc = Convert.ToDouble(flex.Rows[i]["Slxuatdc"]);
                    phieunhapVO.Dauky = 0;
                    if (flex.Rows[i]["losanxuat"] != null && flex.Rows[i]["handung"] != null)
                    {
                        phieunhapVO.Losanxuat = flex.Rows[i]["losanxuat"].ToString();
                        phieunhapVO.Handung = Convert.ToDateTime(flex.Rows[i]["handung"]);
                        phieunhapVO.Keysl = Modules.CreateRunninNo(new Random());
                    }
                    else
                    {
                        MessageBox.Show("NhËp h¹n dïng vµ l« s¶n xuÊt");
                        break;
                    }
                    if (phieunhapBUS.CheckPhieuNhap(phieunhapVO).Rows.Count == 0)
                    {                        
                        phieunhapBUS.InsertRecord(phieunhapVO);
                    }
                    else
                    {
                        phieunhapBUS.UpdateRecord(phieunhapVO);
                    }
                        
                    //flex.Rows[i]["slcuoiky"] = phieunhapVO.Sldauky + phieunhapVO.Slnhap - slxuat - Convert.ToInt32(flex.Rows[i]["slxuatdc"]);
                    flex.Rows[i]["slNhapcu"] = flex.Rows[i]["slNhap"];
                    flex.Rows[i]["slxuatdccu"] = flex.Rows[i]["slxuatdc"];
                    
                   
                }
            }
            Cursor.Current = Cursors.Default;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            cboNhomCP.SelectedValue  = "";
            cboKy.SelectedValue = "";
            txtMahieu.Text = "";
            txtTenhieu.Text = "";
            flex.Rows.Count = 1;
        }

        private void btnChuyenky_Click(object sender, EventArgs e)
        {
            int thang,nam;
            DataTable dt;
            dt = Modules.GetKy();
            thang = Convert.ToInt32(dt.Rows[0]["thang"]);
            nam = Convert.ToInt32(dt.Rows[0]["Nam"]);
            DateTime tungay = dtNgayNhap.Value;
            DateTime denngay = tungay.AddDays(-1);
            Cursor.Current = Cursors.WaitCursor;
            phieunhapBUS.ChuyenKy(thang, nam, tungay.Month + "/" + tungay.Day + "/" + tungay.Year,
                 tungay.Day + "/" + tungay.Month + "/" + tungay.Year,
                denngay.Month + "/" + denngay.Day + "/" + denngay.Year);
            GetKy();
            btnXoa_Click(null, null);
            Cursor.Current = Cursors.Default;
        }

        private void btnLuyky_Click(object sender, EventArgs e)
        {
            int thang, nam;
            DataTable dt;
            dt = Modules.GetKy();
            thang = Convert.ToInt32(dt.Rows[0]["thang"]);
            nam = Convert.ToInt32(dt.Rows[0]["Nam"]);
            Cursor.Current = Cursors.WaitCursor;
            if (Convert.ToInt16(cboKy.Columns["Ky"].Value) == 1)
            {
                phieunhapBUS.LuiKy(thang, nam);
                GetKy();
                btnXoa_Click(null, null);
            }
            else
            {
                MessageBox.Show("§· cã sè l­îng xuÊt trong kú, kh«ng thÓ lïi kú");
            }
            Cursor.Current = Cursors.Default;
        }

        private void btnGhiDM_Click(object sender, EventArgs e)
        {
            PhieunhapVO phieunhapVO = new PhieunhapVO();
            Cursor.Current = Cursors.WaitCursor;
            for (int i = 1; i < flex.Rows.Count; i++)
            {
                if (flex.Rows[i]["nhomthuoc"] != flex.Rows[i]["nhomthuoccu"] || flex.Rows[i]["bietduoc"] != flex.Rows[i]["bietduoccu"])
                {
                    phieunhapVO.Sttnh = cboNhomCP.Columns[1].Value.ToString();
                    phieunhapVO.Mahieu = flex.Rows[i]["Mahieu"].ToString();
                    phieunhapVO.Nhomthuoc = flex.Rows[i]["nhomthuoc"].ToString();
                    phieunhapVO.Bietduoc = flex.Rows[i]["bietduoc"].ToString();
                    phieunhapBUS.UpdateDM (phieunhapVO);
                    flex.Rows[i]["nhomthuoccu"] = phieunhapVO.Nhomthuoc;
                    flex.Rows[i]["bietduoccu"] = phieunhapVO.Bietduoc;
                                         
                }
            }
            Cursor.Current = Cursors.Default;
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            DataRow newRow = null;
            Dataset.dsBCNX ds = new Dataset.dsBCNX();
            Cursor.Current = Cursors.WaitCursor;
            DataTable dataTable;
            KyVO kyVO = Modules.GetKy(Convert.ToInt32(cboKy.Columns[1].Value), Convert.ToInt32(cboKy.Columns[2].Value));
            PhieuNhapDAO phieunhapDAO = new PhieuNhapDAO();
                dataTable = phieunhapDAO.SearchByNgay(Convert.ToInt32(cboKy.Columns[1].Value),
                    Convert.ToInt32(cboKy.Columns[2].Value), "('01','03')",
                    dtTungay.Value.Month + "/" + dtTungay.Value.Day + "/" + dtTungay.Value.Year,
                    dtDenngay.Value.Month + "/" + dtDenngay.Value.Day + "/" + dtDenngay.Value.Year, "");
           
            foreach (DataRow dr in dataTable.Rows)
            {
                newRow = ds.Tables[0].NewRow();
                newRow["Nhomthuoc"] = Converter.TCVN3ToUnicode(dr["Nhomthuoc"].ToString()).ToUpper();
                newRow["Tenthuoc"] = dr["Ten_hieu"].ToString();
                newRow["bietduoc"] = dr["bietduoc"].ToString();
                newRow["Hamluong"] = Convert.ToString(dr["ham_luong"]);
                newRow["nuocsx"] = dr["nuoc_sx"].ToString();
                newRow["DVT"] = dr["Dvt"].ToString();
                newRow["Dongia"] = Convert.ToDouble(dr["gia"]);

                newRow["TonCK"] = Convert.ToInt32(dr["Slcuoiky"]);
                newRow["TIENCK"] = Convert.ToInt32(dr["Slcuoiky"]) * Convert.ToDouble(newRow["Dongia"]);

                newRow["TonDK"] = Convert.ToInt32(dr["Sldauky"]);
                newRow["TienDK"] = Convert.ToInt32(dr["Sldauky"]) * Convert.ToDouble(newRow["Dongia"]);

                newRow["SLNhap"] = Convert.ToInt32(dr["Slnhap"]);
                newRow["TienNhap"] = Convert.ToInt32(dr["Slnhap"]) * Convert.ToDouble(newRow["Dongia"]);

                newRow["SLXuat"] = Convert.ToInt32(dr["Slxuat"]) + Convert.ToInt32(dr["Slxuatdc"]);
                newRow["TienXuat"] = (Convert.ToInt32(dr["Slxuat"]) + Convert.ToInt32(dr["Slxuatdc"])) * Convert.ToDouble(newRow["Dongia"]);
                ds.Tables[0].Rows.Add(newRow);

            }
            Cursor.Current = Cursors.Default;
            Report f = new Report();
            ReportDocument cr = new ReportDocument();
            cr.Load("rptBCXN.rpt");
            cr.SetDataSource(ds);
            foreach (CrystalDecisions.CrystalReports.Engine.FormulaFieldDefinition thisFormulaField in cr.DataDefinition.FormulaFields)
            {
                switch (thisFormulaField.Name)
                {
                    case "thang":
                        thisFormulaField.Text = "'" + kyVO.Thang + "'";
                        break;
                    case "nam":
                        thisFormulaField.Text = "'" + kyVO.Nam + "'";
                        break;
                    case "tungay":
                        thisFormulaField.Text = "'" + kyVO.Tungay.Substring(0,10) + "'";
                        break;
                    case "denngay":
                        thisFormulaField.Text = "'" + kyVO.Denngay.Substring(0, 10) + "'";
                        break;

                }
            }
            f.crpView.ReportSource = cr;
            f.Show();

        }

        private void flex_AfterEdit(object sender, C1.Win.C1FlexGrid.RowColEventArgs e)
        {
            if (flex.Cols[e.Col].Name == "Nhap")
            {
                if (flex.Rows[e.Row][e.Col].ToString() != "")
                {
                    if (flex.Rows[e.Row]["slnhapcu"] == null || flex.Rows[e.Row]["slnhapcu"].ToString() == "")
                    {
                        flex.Rows[e.Row][e.Col + 1] = flex.Rows[e.Row][e.Col];
                    }
                    else
                    {
                        flex.Rows[e.Row][e.Col + 1] = Convert.ToInt32(flex.Rows[e.Row]["slnhapcu"]) + Convert.ToInt32(flex.Rows[e.Row][e.Col]);
                    }
                    flex.Rows[e.Row]["Slcuoiky"] = Convert.ToInt32(flex.Rows[e.Row]["Sldauky"]) + Convert.ToInt32(flex.Rows[e.Row]["Slnhap"]) - Convert.ToInt32(flex.Rows[e.Row]["Slxuat"]) - Convert.ToInt32(flex.Rows[e.Row]["Slxuatdc"]);
                }
                else
                {
                    flex.Rows[e.Row][e.Col + 1] = flex.Rows[e.Row]["slnhapcu"];
                }
            }
            else if (flex.Cols[e.Col].Name == "xuat")
            {
                if (flex.Rows[e.Row][e.Col].ToString() != "")
                {
                    if (flex.Rows[e.Row]["slxuatdccu"] == null || flex.Rows[e.Row]["slxuatdccu"].ToString() == "")
                    {
                        flex.Rows[e.Row][e.Col + 1] = flex.Rows[e.Row][e.Col];
                    }
                    else
                    {
                        flex.Rows[e.Row][e.Col + 1] = Convert.ToInt32(flex.Rows[e.Row]["slxuatdccu"]) + Convert.ToInt32(flex.Rows[e.Row][e.Col]);
                      }
                    flex.Rows[e.Row]["Slcuoiky"] = Convert.ToInt32(flex.Rows[e.Row]["Sldauky"]) + Convert.ToInt32(flex.Rows[e.Row]["Slnhap"]) - Convert.ToInt32(flex.Rows[e.Row]["Slxuat"]) - Convert.ToInt32(flex.Rows[e.Row]["Slxuatdc"]);
                }
                else
                {
                    flex.Rows[e.Row][e.Col + 1] = flex.Rows[e.Row]["slxuatdccu"];
                }
            }
        }

        private void btnNgaynhap_Click(object sender, EventArgs e)
        {
            if (flex.Selection.TopRow > 0)
            {
                Cursor.Current = Cursors.WaitCursor;
                frmNgaynhap childForm = new frmNgaynhap();
                childForm.mahieu = flex.Rows[flex.Selection.TopRow]["mahieu"].ToString();
                childForm.tenhieu = flex.Rows[flex.Selection.TopRow]["tenhieu"].ToString();
                childForm.thang = Convert.ToInt32(cboKy.Columns[1].Value);
                childForm.nam = Convert.ToInt32(cboKy.Columns[2].Value);

                if (flex.Cols[flex.Selection.LeftCol].Name.ToUpper() == "SLNHAP")
                {
                    childForm.loai = 0;
                }
                else if (flex.Cols[flex.Selection.LeftCol].Name.ToUpper() == "SLXUAT")
                {
                    childForm.loai = 1;
                }
                else if (flex.Cols[flex.Selection.LeftCol].Name.ToUpper() == "SLXUATDC")
                {
                    childForm.loai = 2;
                }
                childForm.ShowDialog();
                Cursor.Current = Cursors.Default;
            }
            
        }

    

      

        

    }
}
